package com.qingcloud.adminbackend.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.product.SysProductCommandDto;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.adminbackend.base.AjaxResult;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 导入文件模板公共类
 */
public class ImportFileUtil {

    /**
     * 解析导入模板文件第一列设备编号列表
     */
    public static AjaxResult parserEquipmentIdForExcel(InputStream input, List<String> equipmentIdList) {
        try {
            Workbook workbook = new XSSFWorkbook(input);
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet != null) {
                for (int rowNum = 1; rowNum <= sheet.getPhysicalNumberOfRows(); rowNum++) {
                    Row row = sheet.getRow(rowNum);
                    if (row != null) {
                        String equipmentId = ExcelUtil.getCellValue(row.getCell(0));
                        if (StringUtils.isNotBlank(equipmentId) && !"".equals(equipmentId.replace(".0000", "").trim())) {
                            equipmentId = equipmentId.replace(".0000", "").trim();
                            if (equipmentIdList.contains(equipmentId)) {
                                return new AjaxResult(WamingConstant.FAILURE, "第" + rowNum + "行设备编号重复");
                            } else {
                                equipmentIdList.add(equipmentId);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult(WamingConstant.FAILURE, "模板解析错误", null);
        }
        if (equipmentIdList.isEmpty()) {
            return new AjaxResult(WamingConstant.FAILURE, "设备编号不能为空", null);
        }
        return new AjaxResult(WamingConstant.SUCCESS, null, null);
    }

    /**
     * 指令导入文件解析
     */
    public static List<SysProductCommandDto> productCommandImportParse(InputStream inputStream, Integer classifyId, Integer productTypeId) {
        final ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(inputStream);
        final List<List<Object>> lineList = reader.read();
        List<SysProductCommandDto> dtoList = new ArrayList<>(lineList.size() - 1);
        //跳过excel第一行
        for (int i = 1; i < lineList.size(); i++) {
            final SysProductCommandDto dto = new SysProductCommandDto();
            List<Object> columnList = lineList.get(i);
            //指令名称
            String name = columnList.get(0).toString();
            if (StrUtil.isBlank(name)) {
                throw new WmCommonException("第" + i + "行指令名称不能为空");
            }
            dto.setCommandName(name);
            //指令英文
            String module = columnList.get(1).toString();
            if (StrUtil.isBlank(name)) {
                throw new WmCommonException("第" + i + "行指令英文名称不能为空");
            }
            dto.setCommandModule(module);
            Object desc = columnList.get(2);
            if (Objects.nonNull(desc)) {
                dto.setDescription(desc.toString());
            }
            Object waitResp = columnList.get(3);
            if (Objects.nonNull(waitResp)) {
                if (waitResp.toString().contains("是")) {
                    dto.setWaitResponse(1);
                } else {
                    dto.setWaitResponse(2);
                }
            }
            //其他信息
            dto.setProductTypeId(productTypeId);
            dto.setProductCommandClassifyId(classifyId);
            dtoList.add(dto);
        }
        return dtoList;
    }
}
